package cmp.compile.panels;

import java.awt.Component;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import cmp.main.Globals;

public class MethodTransitivePanel extends JPanel {
	private static final long serialVersionUID = 109004697047687473L;	

	public MethodTransitivePanel(CompilePanel parentPanel) {
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		setAlignmentX(Component.LEFT_ALIGNMENT);
		setBackground(Globals.BGCOLOR);
		int width = Globals.CompilePanel.WIDTH;
		add(Box.createVerticalStrut(20));
		
		// prefix
		JPanel row = CompilePanel.getPanel(true);
		row = CompilePanel.getPanel(true);
		lblPrefix = new JLabel("Prefix");
		row.add(lblPrefix);
		row.add(Box.createHorizontalStrut(width - lblPrefix.getPreferredSize().width));
		
		txtPrefix = new JTextField(3);
		txtPrefix.setMaximumSize(txtPrefix.getPreferredSize());
		txtPrefix.setMinimumSize(txtPrefix.getPreferredSize());
		row.add(txtPrefix);
		row.add(Box.createHorizontalStrut(5));
		
		JLabel req = new JLabel("required");
		row.add(req);
		add(row);
		add(Box.createVerticalStrut(10));
				
		// name
		row = CompilePanel.getPanel(true);
		lblName = new JLabel("Name");
		row.add(lblName);
		row.add(Box.createHorizontalStrut(width - lblName.getPreferredSize().width));
		
		txtName = new JTextField(20);
		txtName.setText(Globals.Methods.Transitive.TYPE_NAME);
		txtName.setMaximumSize(txtName.getPreferredSize());
		txtName.setMinimumSize(txtName.getPreferredSize());
		row.add(txtName);
		add(row);
		add(Box.createVerticalStrut(10));
			
		// Remark
		row = CompilePanel.getPanel(true);
		lblRemark = new JLabel("Remark");
		row.add(lblRemark);
		row.add(Box.createHorizontalStrut(width - lblRemark.getPreferredSize().width));
			
		txtRemark = new JTextField(20);
		txtRemark.setMaximumSize(txtRemark.getPreferredSize());
		txtRemark.setMinimumSize(txtRemark.getPreferredSize());
		row.add(txtRemark);		
		row.add(Box.createHorizontalStrut(5));
		
		JLabel eg = new JLabel("e.g. parameters");
		row.add(eg);
		add(row);
		add(Box.createVerticalStrut(10));
		
		// cutoff
		row = CompilePanel.getPanel(true);
		lblOverlapCutoff = new JLabel("Overlap cutoff");
		row.add(lblOverlapCutoff);
		row.add(Box.createHorizontalStrut(width - lblOverlapCutoff.getPreferredSize().width));
		
		txtOverlapCutoff = new JTextField(4);
		txtOverlapCutoff.setText(Globals.Methods.Transitive.OVERLAP_CUTOFF);
		txtOverlapCutoff.setMaximumSize(txtOverlapCutoff.getPreferredSize());
		txtOverlapCutoff.setMinimumSize(txtOverlapCutoff.getPreferredSize());
		row.add(txtOverlapCutoff);	
		add(row);
		add(Box.createVerticalStrut(10));
		
		// similarity
		row = CompilePanel.getPanel(true);
		lblSimilarity = new JLabel("Similarity");
		row.add(lblSimilarity);
		row.add(Box.createHorizontalStrut(width - lblSimilarity.getPreferredSize().width));
		
		txtSimilarity = new JTextField(4);
		txtSimilarity.setText(Globals.Methods.Transitive.SIMILARITY);
		txtSimilarity.setMaximumSize(txtSimilarity.getPreferredSize());
		txtSimilarity.setMinimumSize(txtSimilarity.getPreferredSize());
		row.add(txtSimilarity);	
		add(row);
		add(Box.createVerticalStrut(10));
		
		setMaximumSize(getPreferredSize());
		setMinimumSize(getPreferredSize());
	}
	
	public String getSettings() {
		return  "x:" + txtOverlapCutoff.getText() + ":" + txtSimilarity.getText() + ":x";
	}
	
	public void setSettings(String settings) {
		String [] theSettings = settings.split(":");
		txtOverlapCutoff.setText(theSettings[1]);
		txtSimilarity.setText(theSettings[2]);
	}
	
	public void resetSettings() {
		txtName.setText(Globals.Methods.Transitive.TYPE_NAME);
		txtPrefix.setText(Globals.Methods.Transitive.DEFAULT_PREFIX);
		txtRemark.setText("");
		txtOverlapCutoff.setText(Globals.Methods.Transitive.OVERLAP_CUTOFF);
		txtSimilarity.setText(Globals.Methods.Transitive.SIMILARITY);
	}
	
	public String getMethodName() { return txtName.getText(); }
	public void setMethodName(String name) { txtName.setText(name); }
	
	public static String getMethodType() { return Globals.Methods.Transitive.TYPE_NAME; }
	
	public String getPrefix() { return txtPrefix.getText(); }
	public void setPrefix(String prefix) { txtPrefix.setText(prefix); }
	
	public String getComment() { return txtRemark.getText(); }
	public void setComment(String comment) { txtRemark.setText(comment); }
	
	private JLabel lblName = null;
	private JTextField txtName = null;
	private JLabel lblPrefix = null;
	private JTextField txtPrefix = null;
	private JLabel lblRemark = null;
	private JTextField txtRemark = null;
	private JLabel lblOverlapCutoff = null;
	private JTextField txtOverlapCutoff = null;
	private JLabel lblSimilarity = null;
	private JTextField txtSimilarity = null;

	public void setLoaded(boolean bLoaded) {
		lblName.setEnabled(!bLoaded);
		txtName.setEnabled(!bLoaded);
		lblPrefix.setEnabled(!bLoaded);
		txtPrefix.setEnabled(!bLoaded);
		lblOverlapCutoff.setEnabled(!bLoaded);
		txtOverlapCutoff.setEnabled(!bLoaded);
		lblSimilarity.setEnabled(!bLoaded);
		txtSimilarity.setEnabled(!bLoaded);
	}
}
